<?php
    /*
    * Copyright (c) 2007, Osuvaldo Ramos
    * 
    * All rights reserved.
    * 
    * Redistribution and use in source and binary forms, with or without
    * modification, are permitted provided that the following conditions are
    * met:
    *
    *    * Redistributions of source code must retain the above copyright
    *      notice, this list of conditions and the following disclaimer.
    *    * Redistributions in binary form must reproduce the above copyright
    *      notice, this list of conditions and the following disclaimer in the
    *      documentation and/or other materials provided with the distribution.
    *    * Neither the name of the CoreTech nor the names of its
    *      contributors may be used to endorse or promote products derived from
    *      this software without specific prior written permission.
    * 
    * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
    * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
    * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
    * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
    * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
    * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
    * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
    * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
    * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
    * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
    * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    * 
    * $Id: default.php 2 2008-01-21 02:48:28Z xdracox $
    */
    
    if ( !defined('BNETCS') || !defined('ROOT') ) exit();
    
    include_once ROOT.'/mods/RankSystem/'.$CONF['mods']['ranksystem']['name']
        .'.php';
    
    class FrontPage
    {
        var $smarty;
        
        function FrontPage()
        {
            $this->smarty = new Smarty();
            $this->smarty->template_dir = ROOT.'/mods/FrontPage/default';
            $this->smarty->compile_dir = $this->smarty->template_dir;
        }
        
        function display()
        {
            global $CONF;
            
            $news_items = $this->getNewsItems(
                $CONF['mods']['frontpage']['news_items']);
            
            $this->smarty->assign('news_items', $news_items);
            $this->smarty->display('frontpage.tpl');
        }
        
        function getNewsItems($limit = 5)
        {
            global $mdb2;
            
            if ( $limit != NULL )
                $sql = sprintf('SELECT *
                    FROM bnetcs_defaultfp_news
                    ORDER BY news_date
                        DESC
                    LIMIT %d', $limit);
                $sql = 'SELECT *
                    FROM bnetcs_defaultfp_news
                    ORDER BY news_date
                        DESC';
            $result = $mdb2->query($sql);
            
            if ( PEAR::isError($result) )
            {
                $msg = 'MySQL query failed: '.$result->getMessage();
                trigger_error($msg, E_USER_WARNING);
            }
            
            $news_items = array();
            
            while ( $row = $result->fetchRow() )
            {
                $sql = sprintf('SELECT member_login
                    FROM bnetcs_members
                    WHERE member_id = %d', $row['member_id']);
                $member_result = $mdb2->query($sql);
                $member_row = $member_result->fetchRow();
                $poster = $member_row['member_login'];
                
                $news_items[] = array
                (
                    'id' => $row['news_id'],
                    'poster' => $poster,
                    'date' => $row['news_date'],
                    'subject' => $row['news_subject'],
                    'body' => $row['news_body']
                );
            }
            
            return $news_items;
        }
        
        function onLogin($member_id)
        {
        }
        
        function displayConsole()
        {
            global $auth;
            
            $mid = $auth->getAuthData('member_id');
            $actions = array();
            
            if ( RankSystem::getRank($mid) >= RANK_SECOND_LIEUTENANT )
            {
                $actions[] = array
                (
                    'text' => 'Post News',
                    'addr' => 'console.php?a=m&amp;m=FrontPage&amp;p=pn'
                );
                
                $actions[] = array
                (
                    'text' => 'Delete News',
                    'addr' => 'console.php?a=m&amp;m=FrontPage&amp;p=dn'
                );
            }
            
            if ( sizeof($actions) % 2 == 1 )
            {
                $actions[] = array
                (
                    'text' => '',
                    'addr' => ''
                );
            }
            
            $this->smarty->assign('actions', $actions);
            $this->smarty->display('console.tpl');
        }
        
        function displayPage($page)
        {
            global $auth, $mdb2;
            
            $rs = new RankSystem();
            $mid = $auth->getAuthData('member_id');
            
            switch ( $page )
            {
                case 'pn':
                {
                    if ( $rs->getRank($mid) < RANK_SECOND_LIEUTENANT )
                    {
                        $msg = 'You cannot post news.';
                        $_SESSION['errors'] = array($msg);
                        header('Location: console.php');
                        return;
                    }
                    
                    
                    if ( isset($_POST['subject']) && isset($_POST['body']) )
                    {
                        $subject = $_POST['subject'];
                        $body = $_POST['body'];
                        $body = strip_tags($body, '<b><i><u><li><ol><ul><br><a>');
                        $body = nl2br($body);
                        if ( !get_magic_quotes_gpc() )
                            $body = addslashes($body);
                        
                        $_SESSION['errors'] = array();
                        
                        if ( !preg_match('#\w{2,255}#', $subject) )
                            $_SESSION['errors'][] = 'Your subject must be'
                                .' between 2 and 255 characters and may only'
                                .' contain normal word characters.';
                        
                        if ( sizeof($_SESSION['errors']) > 0 )
                        {
                            header('Location:console.php?a=m&m=FrontPage&p=pn');
                            return;
                        }
                        
                        $sql = sprintf('INSERT
                            INTO bnetcs_defaultfp_news (member_id, news_subject,
                                news_body, news_date)
                            VALUES (%d, "%s", "%s", %d)',
                            $auth->getAuthData('member_id'), $subject, $body,
                            time());
                        $result = $mdb2->query($sql);
                        
                        if ( PEAR::isError($result) )
                        {
                            $msg = 'MySQL query failed: '.$result->getMessage();
                            trigger_error($msg, E_USER_ERROR);
                        }
                        else
                        {
                            $msg = 'You have successfully posted news.';
                            $_SESSION['notices'] = array($msg);
                            header('Location: console.php');
                        }
                    }
                    else
                    {
                        display_header();
                        $this->smarty->display('postnews.tpl');
                        display_footer();
                    }
                    
                    break;
                }
                
                case 'dn':
                {
                    if ( $rs->getRank($mid) < RANK_SECOND_LIEUTENANT )
                    {
                        $msg = 'You cannot delete news.';
                        $_SESSION['errors'] = array($msg);
                        return;
                    }
                    
                    if ( isset($_GET['n']) )
                    {
                        $news_id = intval($_GET['n']);
                        
                        $sql = sprintf('DELETE FROM bnetcs_defaultfp_news
                            WHERE news_id = %d', $news_id);
                        $result = $mdb2->query($sql);
                        
                        if ( PEAR::isError($result) )
                        {
                            $msg = 'MySQL query failed: '.$result->getMessage();
                            trigger_error($msg, E_USER_ERROR);
                        }
                        else
                        {
                            $msg = 'Successfully deleted news item.';
                            $_SESSION['notices'] = array($msg);
                            header('Location: console.php');
                        }
                    }
                    else
                    {
                        display_header();
                        $items = $this->getNewsItems(NULL);
                        $this->smarty->assign('news_items', $items);
                        $this->smarty->display('delnews.tpl');
                        display_footer();
                    }
                }
            }
        }
    }
?>
